package com.pifuke.patient.ui;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.o.app.io.Extra;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.pifuke.patient.R;
import com.pifuke.patient.SymptomQuestionActivity;
import com.smiier.skin.QuestionDetailActivity;
import com.smiier.skin.adapter.QuestionAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.net.QuestionGetListTask.QuestionGetListRequest;
import com.smiier.skin.ui.BasicStateView;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.smiier.skin.utils.Util;
import com.umeng.analytics.MobclickAgent;

/**
 * 疾病相关咨询
 * 
 * @author Lennon
 * 
 */
public class SymptomQuestionView extends BasicStateView implements
		OnRefreshListener<ListView>, OnClickListener {

	public static class SymptomQuestionExtra extends Extra {
		public String symptom;
	}

	PullToRefreshListView mPullToRefresh;
	private RelativeLayout mAgeFilter;
	private RelativeLayout mSexFilter;
	private PopupWindow mPopSex, mPopAge;

	private TextView more_zixun;
	private TextView kepu_zixun_more;
	HashMap<String, Object> params = new HashMap<String, Object>();
	private int[] mLoc = new int[2];
	View mSexView;
	View mAgeView;
	long start_qid;
	int pageIndex;
	TextView mAgeTextView, mSexTextView;
	public static ArrayList<Object> array = new ArrayList<Object>();
	QuestionAdapter adapter;
	private TextView text_empty;
	protected QuestionGetListRequest mRequest = new QuestionGetListRequest();
	private Drawable mArrowUp, mArrowDown;
	static final String mHeadString = "以下是有关“%s”的最新问题";
	static final String mFootString = "查看更多与%s相关的问题";
	TextView mHead;
	TextView mMore;

	public SymptomQuestionView(Context context) {
		super(context);
	}

	public SymptomQuestionView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SymptomQuestionView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		setContentView(R.layout.view_symptom_question);
		mHead = findViewById(R.id.kepu_zixun_title, TextView.class);
		mPullToRefresh = findViewById(R.id.list_question,
				PullToRefreshListView.class);

		View view = LayoutInflater.from(getContext()).inflate(
				R.layout.textview, null);
		mMore = (TextView) view.findViewById(R.id.text);
		mPullToRefresh.getListView().addFooterView(view);
		mMore.setText(String.format(mFootString,
				GlobalSettings.symptomItemInfo.Patient_Name));
		more_zixun = findViewById(R.id.kepu_zixun_more, TextView.class);
		more_zixun.setVisibility(View.VISIBLE);
		more_zixun.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getContext(),
						SymptomQuestionActivity.class);
				SymptomQuestionExtra extra = new SymptomQuestionExtra();
				extra.symptom = GlobalSettings.symptomItemInfo.Patient_Name;
				extra.putTo(intent);
				startActivity(intent);
			}
		});
		init();
		params.put(Constant.PARAM_API, "Question.GetList");
		Resources mRes = getResources();
		mPopSex = Util.getPopWindow(getContext());
		mPopAge = Util.getPopWindow(getContext());
		mAgeTextView = (TextView) findViewById(R.id.age_arrow);
		mSexTextView = (TextView) findViewById(R.id.sex_arrow);
		mSexView = LayoutInflater.from(getContext()).inflate(
				R.layout.sex_filter, null);
		mAgeView = LayoutInflater.from(getContext()).inflate(
				R.layout.age_filter, null);
		mPopSex.setContentView(mSexView);
		mPopAge.setContentView(mAgeView);
		mArrowDown = mRes.getDrawable(R.drawable.arrow_down_white);
		mArrowUp = mRes.getDrawable(R.drawable.arrow_up_white);
		mArrowDown.setBounds(0, 0, mArrowDown.getIntrinsicWidth(),
				mArrowDown.getIntrinsicHeight());
		mArrowUp.setBounds(0, 0, mArrowUp.getIntrinsicWidth(),
				mArrowUp.getIntrinsicHeight());
		text_empty = findViewById(R.id.text_empty, TextView.class);
		// mRequest.pageSize = GlobalSettings.PAGE_SIZE;
		// mRequest.token = GlobalSettings.sToken;
		mRequest.statuses = new ArrayList<Integer>();
		mRequest.statuses.add(3);

		// adapter = new QuestionAdapter();
		// adapter.setDataProvider(new ArrayList<QuestionAnswer>());
		mRequest.Symptoms = new ArrayList<String>();
		mRequest.keyWord = GlobalSettings.symptomItemInfo.Patient_Name;
		params.put("keyword", GlobalSettings.symptomItemInfo.Patient_Name);
		mRequest.token = GlobalSettings.sToken;
		// mPullToRefresh.setAdapter(adapter);

		kepu_zixun_more = findViewById(R.id.kepu_zixun_more, TextView.class);
		// more_zixun = findViewById(R.id.look_more_zixun, TextView.class);

		mPopAge.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				mAgeTextView.setCompoundDrawables(null, null, mArrowDown, null);
			}
		});
		mPopSex.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				mSexTextView.setCompoundDrawables(null, null, mArrowDown, null);
			}
		});
		mPullToRefresh.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int arg2, long arg3) {

				JSONObject object = (JSONObject) view.getTag(R.id.tag_obj);
				if (object == null) {
					Intent intent = new Intent(getContext(),
							SymptomQuestionActivity.class);
					SymptomQuestionExtra extra = new SymptomQuestionExtra();
					extra.symptom = GlobalSettings.symptomItemInfo.Patient_Name;
					extra.putTo(intent);
					startActivity(intent);
				} else {
					Intent intent = new Intent(getContext(),
							QuestionDetailActivity.class);
					intent.putExtra(Constant.IDENTITY_KEY, object.toString());
					startActivity(intent);
				}

			}
		});

	}

	@Override
	public void onResume() {
		super.onResume();
		mHead.setText(String.format(mHeadString, mRequest.keyWord));

		MobclickAgent.onResume(getContext());

		loadData1();
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();

	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		start_qid = 0;
		pageIndex = 0;
		array.clear();
		loadData1();
	}

	private void loadData1() {
		array.clear();
		params.put("startQid", 0);
		params.put("pageSize", 10);
		RequestTaskIntercept.requestIntercept(getContext(),
				Constant.URL.REQUEST, new RequestHandler(getContext(),
						new HandlerCallback() {

							@Override
							public void callback(Object o) {
								mPullToRefresh.onPullDownRefreshComplete();
								mPullToRefresh.onPullUpRefreshComplete();
								mPullToRefresh.setLastUpdateTime();
								if (!CommonUtility.isNull(o)) {
									JSONObject object = (JSONObject) o;
									if (CommonUtility.response200(object)) {
										try {

											object = object
													.getJSONObject("Res");
											JSONArray arr = (JSONArray) object
													.getJSONArray("Questions");
											if (arr.length() > 0) {

												CommonUtility.putAll(array,
														arr, true);
												mPullToRefresh
														.setHasMoreData(true);
											} else {
												if (array.size() > 0) {
													mPullToRefresh
															.setHasMoreData(false);
												} else {

												}
											}
											if (array.size() > 0) {
												start_qid = ((JSONObject) array
														.get(array.size() - 1))
														.getInt("Qid");
												// text_empty.setVisibility(View.GONE);
											} else {
												// text_empty.setVisibility(View.VISIBLE);
											}
											if (CommonUtility.isNull(adapter)) {
												adapter = new QuestionAdapter(
														(Activity) getContext(),
														array);
												mPullToRefresh
														.setAdapter(adapter);
											} else {
												adapter.notifyUpdate();
											}
											if (array.size() < 1) {

												mPullToRefresh
														.setContextEmptyType(
																true, 2);
											} else {

												mPullToRefresh
														.setContextEmptyType(
																false, 2);
											}
										} catch (Exception e) {
											e.printStackTrace();
										}
									} else {
										mPullToRefresh.setContextEmptyType(
												true, 2);
										CommonUtility.toast(getContext(),
												R.string.server_unavailable);
									}
								} else {
									mPullToRefresh.setContextEmptyType(true, 3);
								}
							}
						}), false, params);
	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {
	}

	/*
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */

}
